package com.shopping.www.service;

import com.shopping.www.mapper.BusinessMapper;
import com.shopping.www.model.*;
import com.shopping.www.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class BusinessService {
    SqlSessionFactory factory= SqlSessionFactoryUtils.getSqlSessionFactory();
    public BusinessBrand<Brand> selectShelveBrands(int business_id){
        SqlSession sqlSession=factory.openSession();
        BusinessMapper mapper =sqlSession.getMapper(BusinessMapper.class);
        int[] shelveIds=mapper.selectIds(business_id);
//        System.out.println(shelveIds);
        BusinessBrand<Brand> businessBrand=new BusinessBrand<>();
        if(shelveIds.length>0) {
            List<Brand> brands= mapper.selectShelveBrands(shelveIds);
            List<Brand> brandList=new ArrayList<>();
            for (int i = 0; i < shelveIds.length; i++) {
                for(int j=0;j<brands.size();j++) {
                    if (brands.get(j).getId() == shelveIds[i]) {
                        brandList.add(brands.get(j));
                    }
                }
            }
            int[] shelveNums=mapper.selectShelveNums(shelveIds);
            businessBrand.setBrands(brandList);
            businessBrand.setNums(shelveNums);
            sqlSession.close();
            return businessBrand;
        }else{
            sqlSession.close();
            return businessBrand;
        }
    }
    public void leaveFromShelve(int business_id,int brand_id){
        SqlSession sqlSession=factory.openSession();
        BusinessMapper mapper =sqlSession.getMapper(BusinessMapper.class);
        System.out.println(business_id);
        System.out.println(brand_id);
        int shelveNum= mapper.selectBusinessNum(brand_id,business_id);
        System.out.println(shelveNum);
            int wareNum = mapper.selectWareNum(brand_id, business_id);
            mapper.leaveFromShelve(business_id, brand_id, shelveNum, wareNum);
            sqlSession.commit();
            sqlSession.close();
    }
    public BusinessBrand<Brand> selectWareBrands(int business_id){
        SqlSession sqlSession=factory.openSession();
        BusinessMapper mapper =sqlSession.getMapper(BusinessMapper.class);
        int[] wareIds=mapper.selectWareIds(business_id);
        BusinessBrand<Brand> businessBrand=new BusinessBrand<>();
        if(wareIds.length>0) {
            List<Brand> brands= mapper.selectWareBrands(wareIds);
            List<Brand> brandList=new ArrayList<>();
            for (int i = 0; i < wareIds.length; i++) {
                for(int j=0;j<brands.size();j++) {
                    if (brands.get(j).getId() == wareIds[i]) {
                        brandList.add(brands.get(j));
                    }
                }
            }
            int[] wareNums=mapper.selectWareNums(wareIds,business_id);
            businessBrand.setBrands(brandList);
            businessBrand.setNums(wareNums);
            sqlSession.close();
            return businessBrand;
        }else{
            sqlSession.close();
            return businessBrand;
        }
    }
    public void PutOnShelve(int brand_id,int num,int price,String belong,String description,int business_id){
        SqlSession sqlSession=factory.openSession();
        BusinessMapper mapper =sqlSession.getMapper(BusinessMapper.class);
        int wareNum=mapper.selectWareNum(brand_id,business_id);
        mapper.PutOnShelve(brand_id,num,wareNum,business_id);
        int id=mapper.selectBusiness_msgId(brand_id,business_id);
        mapper.updateBrandPrice(id,price,description,belong);
        sqlSession.commit();
        sqlSession.close();
    }
    public boolean importBrand(int business_id,String businessName,ImportBrand importBrand){
        SqlSession sqlSession=factory.openSession();
        BusinessMapper mapper =sqlSession.getMapper(BusinessMapper.class);
        int[] brandIds=mapper.selectBrandIdsByBusinessId(business_id);
        int flag=0;
        for (int i = 0; i < brandIds.length; i++) {
            String brandName=mapper.selectBrandNameByBrandId(brandIds[i]);
            if(importBrand.getBrandName().equals(brandName)){
                flag++;
            }
        }
        if(flag!=0){
            return false;
        }else{
            String photo="assets/"+importBrand.getPhoto();
            importBrand.setPhoto(photo);
            mapper.importBrand1(businessName,importBrand);
            sqlSession.commit();
            int maxId=mapper.selectMaxId();
            mapper.importBrand2(business_id,maxId,importBrand);
            sqlSession.commit();
            sqlSession.close();
            System.out.println(maxId);
            return true;
        }
    }
    public void applyLiftBan(int brand_id,String liftBanApply){
        SqlSession sqlSession=factory.openSession();
        BusinessMapper mapper =sqlSession.getMapper(BusinessMapper.class);
        mapper.applyLiftBan(brand_id,liftBanApply);
        sqlSession.commit();
        sqlSession.close();
    }
    public BrandAndNum<Brand> selectBusinessDelivery(int business_id) throws UnsupportedEncodingException {
        SqlSession sqlSession=factory.openSession();
        BusinessMapper mapper =sqlSession.getMapper(BusinessMapper.class);
        //查询商家下的所有商品id
        int[] ids=mapper.selectBrandIds(business_id);
        //根据查询出来的商品ID来查询所有待呼叫的user_msg的id
        int[] deliveryIds=mapper.selectBrandIdsByDeliveryStatus(ids);
        System.out.println(Arrays.toString(ids));
        System.out.println(Arrays.toString(deliveryIds));
        BrandAndNum<Brand> brandAndNum=new BrandAndNum<>();
        if(deliveryIds.length>0) {
            int[] brandIds=mapper.selectBrandIdsByDeliveryIds(deliveryIds);
            int[] deliveryNums=mapper.selectDeliveryNum(deliveryIds);
            System.out.println(Arrays.toString(brandIds));
            System.out.println(Arrays.toString(deliveryNums));
            List<Brand> brandList=new ArrayList<>();
            if(deliveryIds.length==1){
                List<Brand> brands=mapper.selectBrandByBrandIds(brandIds);
                brandAndNum.setBrands(brands);
            }
            if(deliveryIds.length==2){
                if(brandIds[0]==brandIds[1]){
                    brandList.add(mapper.addBrandList(brandIds[0]));
                    brandList.add(mapper.addBrandList(brandIds[0]));
                    brandAndNum.setBrands(brandList);
                }else{
                    List<Brand> brands=mapper.selectBrandByBrandIds(brandIds);
                    brandAndNum.setBrands(brands);
                }
            }
            if(deliveryIds.length>=3) {
                for (int i = 0; i < brandIds.length; i++) {
                    for (int j = i + 1; j < brandIds.length; j++) {
                        if (brandIds[i] == brandIds[j]) {
                            if (brandList.size() <= brandIds.length) {
                                if (i == j - 1)
                                    brandList.add(mapper.addBrandList(brandIds[i]));
                                else
                                    brandList.add(mapper.addBrandList(brandIds[j - 1]));
                            }
                        } else {
                            brandList.add(mapper.addBrandList(brandIds[brandList.size()]));
                        }
                    }
                }
                brandAndNum.setBrands(brandList);
            }
            brandAndNum.setIds(deliveryIds);
            brandAndNum.setNums(deliveryNums);
            sqlSession.close();
            System.out.println(Arrays.asList(brandAndNum.getBrands()));
            return brandAndNum;
        }else{
            sqlSession.close();
            return brandAndNum;
        }
    }
    public void callDelivery(int deliveryId){
        SqlSession sqlSession=factory.openSession();
        BusinessMapper mapper =sqlSession.getMapper(BusinessMapper.class);
        mapper.callDelivery(deliveryId);
        sqlSession.commit();
        sqlSession.close();
    }
    public Boolean checkDeleteWare(int[] selectedIds,int business_id){
        SqlSession sqlSession=factory.openSession();
        BusinessMapper mapper =sqlSession.getMapper(BusinessMapper.class);
        int[] shelveNums=mapper.selectShelveNumsForCheck(selectedIds,business_id);
        System.out.println(Arrays.toString(shelveNums));
        for (int i = 0; i < selectedIds.length; i++) {
            if (shelveNums[i] > 0) {
                return false;
            }
        }
        return true;
    }
    public void deleteWare(int[] selectedIds,int business_id) {
        SqlSession sqlSession = factory.openSession();
        BusinessMapper mapper = sqlSession.getMapper(BusinessMapper.class);
        mapper.deleteWare(selectedIds,business_id);
        sqlSession.commit();
        sqlSession.close();
    }
}
